package com.fand.platform.common.interceptor;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.fand.context.ContextHolder;
import com.fand.platform.authority.service.AuthorityService;
import com.fand.platform.configuration.dto.SiteDto;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.router.support.RouterKeySupport;

/**
 * @author yaoHT
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FanDHandlerInterceptor extends HandlerInterceptorAdapter implements AsyncHandlerInterceptor {

	private RouterKeySupport routerKeySupport;

	private ConfigurationService configurationService;

	private AuthorityService authorityService;

	@Override
	public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response,
			final Object handler, final Exception ex) throws Exception {
	}

	@Override
	public void afterConcurrentHandlingStarted(final HttpServletRequest request, final HttpServletResponse response,
			final Object handler) throws Exception {
	}

	@Override
	public void postHandle(final HttpServletRequest request, final HttpServletResponse response,
			final Object handler, final ModelAndView modelAndView) throws Exception {
		if (modelAndView != null) {
			String viewName = modelAndView.getViewName();

			if (StringUtils.isNotBlank(viewName)) {
				viewName = "default/" + viewName;

				modelAndView.setViewName(viewName);
			}
		}
	}

	@Override
	public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response,
			final Object handler) throws Exception {
		ContextHolder.setId(request.getSession().getId());

		final String servletPath = request.getServletPath();

		if (StringUtils.startsWith(servletPath, "/resources")) {
			return true;
		}

		this.routerKeySupport.setGroupId(ContextHolder.getId(), "dev");

		final String domainUrl = request.getServerName();

		SiteDto site = this.configurationService.getSiteService().getSiteByDomainUrl(domainUrl);

		if (site == null) {
			site = this.configurationService.getSiteService().getSiteByDefault();
		}

		request.setAttribute("fandSite", site);

		request.setAttribute("fandServletPath", servletPath);

		Map<String, String> variableMap = this.configurationService.getVariableService().getVariableByTypeCoForMap(
				"url");

		if (!org.springframework.util.CollectionUtils.isEmpty(variableMap)) {
			for (final Entry<String, String> variableEntry : variableMap.entrySet()) {
				request.setAttribute(variableEntry.getKey(), variableEntry.getValue());
			}
		}

		return true;
	}
}
